// 异步操作和修改的封装
import * as types from './mutation-types'
import {playMode} from 'common/js/config'
import {shuffle} from 'common/js/util'
import {saveSearch, clearSearch, deleteSearch, savePlay, saveFavorite, deleteFavorite} from 'common/js/cache'
function findIndex(list, song) {
  return list.findIndex((item) => {
    return item.id === song.id
  })
}
// 选择当前播放的歌曲
export const selectPlay = function ({commit, state}, {list, index}) {
  commit(types.SET_SEQUENCE_LIST, list)
  if (state.mode === playMode.random) {
    let randomList = shuffle(list)
    commit(types.SET_PLAYLIST, randomList)
    index = findIndex(randomList, list[index])
  } else {
    commit(types.SET_PLAYLIST, list)
  }
  commit(types.SET_CURRENT_INDEX, index)
  commit(types.SET_FULL_SCREEN, true)
  commit(types.SET_PLAYING_STATE, true)
}
// 随机播放全部
export const randomPlay = function ({commit}, {list}) {
  // 设置播放模式为随机播放
  commit(types.SET_PLAY_MODE, playMode.random)
  // 设置当前原始播放列表
  commit(types.SET_SEQUENCE_LIST, list)
  // 随机之后的播放列表
  let randomList = shuffle(list)
  commit(types.SET_PLAYLIST, randomList)
  // 设置当前播放的索引
  commit(types.SET_CURRENT_INDEX, 0)
  // 设置当前播放是否为全屏播放
  commit(types.SET_FULL_SCREEN, true)
  // 设置当前播放的状态
  commit(types.SET_PLAYING_STATE, true)
}
// 将搜索得到的歌曲插入到播放列表
export const insertSong = function ({commit, state}, song) {
  // 将变化的三个参数
  let playlist = state.playlist.slice()
  let sequenceList = state.sequenceList.slice()
  let currentIndex = state.currentIndex
  // 记录当前歌曲
  let currentSong = playlist[currentIndex]
  // 查找当前列表中是否有待插入的歌曲并返回其索引
  let fpIndex = findIndex(playlist, song)
  // 因为是插入歌曲，所以索引+1
  currentIndex++
  // 插入这首歌到当前索引位置
  playlist.splice(currentIndex, 0, song)
  // 如果已经包含了这首歌
  if (fpIndex > -1) {
    // 如果当前插入的序号大于列表中的序号
    if (currentIndex > fpIndex) {
      // 删除列表中的查找得到到的歌曲[1,2,3,4,2]则索引为1 的2被删除 最后一个2插入之
      playlist.splice(fpIndex, 1)
      // 列表的legnth将-1
      currentIndex--
    } else {
      // 如果当前插入的序号小于列表中的序号，就要删除查找得到的歌曲
      // [1,2,3,4,2] 第一个2是插入的歌曲，则要删除当前列表中的2 的索引
      // 在当前参数后增加一个
      playlist.splice(fpIndex + 1, 1)
    }
  }
  // 当前歌曲在sequenceList插入的位置
  let currentSIndex = findIndex(sequenceList, currentSong) + 1
  // 查找将要插入的歌曲在sequenceList中的位置
  let fsIndex = findIndex(sequenceList, song)
  // 将歌曲插入到sequenceList中
  sequenceList.splice(currentSIndex, 0, song)
  // 如果插入的歌曲在sequenceList中
  if (fsIndex > -1) {
    // 带插入的歌曲索引大于查找的索引，则删除小的索引
    if (currentSIndex > fsIndex) {
      sequenceList.splice(fsIndex, 1)
    } else {
      // 删除 索引值大的值（因为增加了一个值，所以索引需要+1）
      sequenceList.splice(fsIndex + 1, 1)
    }
  }
  // 重置列表
  commit(types.SET_PLAYLIST, playlist)
  commit(types.SET_SEQUENCE_LIST, sequenceList)
  commit(types.SET_CURRENT_INDEX, currentIndex)
  commit(types.SET_FULL_SCREEN, true)
  commit(types.SET_PLAYING_STATE, true)
}
// 保存搜索历史
export const saveSearchHistory = function ({commit}, query) {
  commit(types.SET_SEARCH_HISTORY, saveSearch(query))
}
// 删除单条搜索的结果
export const deleteSearchHistory = function ({commit}, query) {
  commit(types.SET_SEARCH_HISTORY, deleteSearch(query))
}
// 删除所有搜索数据
export const clearSearchHistory = function ({commit}) {
  commit(types.SET_SEARCH_HISTORY, clearSearch())
}
// 删除单条歌曲
export const deleteSong = function ({commit, state}, song) {
  // 需要修改playlist，sequenceList，currentIndex
  let playlist = state.playlist.slice()
  let sequenceList = state.sequenceList.slice()
  let currentIndex = state.currentIndex
  // 知道当前歌曲的索引
  let pIndex = findIndex(playlist, song)
  // 删除当前歌曲
  playlist.splice(pIndex, 1)
  // 找到当前歌曲在播放列表中的歌曲索引
  let sIndex = findIndex(sequenceList, song)
  // 删除当前索引
  sequenceList.splice(sIndex, 1)
  // 当前歌曲索引在playlist中找到的索引的后面，或者当前歌曲的索引是播放列表的最后一首歌
  if (currentIndex > pIndex || currentIndex === playlist.length) {
    currentIndex--
  }
  // 提交重新得到的数据
  commit(types.SET_PLAYLIST, playlist)
  commit(types.SET_SEQUENCE_LIST, sequenceList)
  commit(types.SET_CURRENT_INDEX, currentIndex)
  // 如果整个列表没有数据的时候，需要重置当前列表状态
  if (!playlist.length) {
    commit(types.SET_PLAYING_STATE, false)
  } else {
    commit(types.SET_PLAYING_STATE, true)
  }
  const playingState = playlist.length > 0
  commit(types.SET_PLAYING_STATE, playingState)
}
// 清空播放列表
export const deleteSongList = function ({commit}) {
  commit(types.SET_CURRENT_INDEX, -1)
  commit(types.SET_PLAYLIST, [])
  commit(types.SET_SEQUENCE_LIST, [])
  commit(types.SET_PLAYING_STATE, false)
}
// 保存历史播放记录
export const savePlayHistory = function ({commit}, song) {
  commit(types.SET_PLAY_HISTORY, savePlay(song))
}
// 保存收藏列表
export const saveFavoriteList = function ({commit}, song) {
  commit(types.SET_FAVORITE_LIST, saveFavorite(song))
}
// 删除收藏列表
export const deleteFavoriteList = function ({commit}, song) {
  commit(types.SET_FAVORITE_LIST, deleteFavorite(song))
}
